<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/include/base.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<link rel="shortcut icon" href="img/favicon.png"/>
<script  type="text/javascript"	src="static/js/pageComm.js"></script>
<script  type="text/javascript"	src="${APP_PATH}/static/js/pageListComm.js"></script>

</head>

  <body>
 
  <!-- container section start -->
  <section id="container" class="">
      <%@include file="/WEB-INF/include/top.jsp"%>

      <%@include file="/WEB-INF/include/left.jsp"%>

      <!--main content start-->
      <section id="main-content" >
          <section class="wrapper">
		  <div class="row">
				<div class="col-lg-12">
					<ol class="breadcrumb">
						<li><i class="fa fa-home"></i><a href="index.jsp">人员管理</a></li>
						<li><i class="fa fa-table"></i>人员信息</li>
					</ol>
					
				</div>
			</div>
	          
             
            
            
              <div class="row" align="center" style="margin:0 auto">
                  <div class="col-lg-12">
                      <section class="panel">
                          <table class="table table-striped table-advance table-hover" id="personTable" >
                          	<thead>
                          
                          		 <tr id="target" target="${target }">
                          		
                          		 <c:set value="" var="target" scope="session"></c:set>
                                 <th width="8%" style="text-align:center;"><input type="checkbox" id="checkAll" /></th>
                                 <th width="14%">ID</th>
                                 <th width="14%">姓名</th>
                                 <th width="10%">负责人</th>
                                 <th width="19%">所属部门</th>
                                 <th width="16%">人员类型</th>
                                  <th width="17%" style="text-align: center;"><span>操作&nbsp;&nbsp;&nbsp;</span>
                                 	<span class="btn-group" >
                                      <a class="btn btn-primary"  onclick="location.href='person/person-add.jsp'">新增</a>
                                      <a class="btn btn-danger" id="deletePerson">删除</a>
                      				</span>
                                 </th>
                              </tr>
                          	
                          	</thead>
                          
                           <tbody>
                      
                           </tbody>
                           <tfoot>
                           <tr>
                           		<td colspan="7">
                           			<div class="col-md-6" id="page_info_area"></div>
								<!-- 分页条信息 -->
							<div class="col-md-6" id="page_nav_area" ></div>
                           		</td>
                           </tr>
                           	
                           	
                           </tfoot>
                        </table>
                      </section>
                  </div>
              </div>
              
              <script  type="text/javascript" >
              
              var currentPage ;
                   $(function(){
                	//  alert($("#target").attr("target"));
                	   if($("#target").attr("target") == null || $("#target").attr("target") ==""){
                		 //去首页
                		   to_page(1); 
                	   }else{
                		   to_page($("#target").attr("target"));
                	   }
                	   
                   }); 
                   
                 //去指定的分页
         	      function to_page(pn){
         	    	 
         	      		$.ajax({
         	      			url:"${APP_PATH}/person",
         	      			data:"pn="+pn,
         	      			type:"GET",
         	      			success:function(result){
         	      				
         	      			//2. 解析并显示分页数据
          	 				   build_page_info(result);
          	 				 //3解析显示分页条
         	 				   build_page_nav(result);
          	 				 
         	      				//1. 解析并显示员工数据
         	 				   build_emps_table(result);
         	 				   
         	 				  // alert(" build_page_info(result);");
         	 				  
         	      			}
         	      		});	
         	      }

         	 	//1. 解析并显示员工数据
         			function build_emps_table(result){
         				//清空table表格
         		  		$("#personTable tbody").empty();
         				//获取返回的dept值
         				var person=result.extend.pageInfo.list;
         				
         				$.each(person,function(index,item){
         					var checkboxTd=$("<td></td>").append($("<input></input>")
         							.addClass("checkbox")
         							.attr("username",item.username)
         							.attr("userId",item.id)
         							.attr( "type","checkbox")).attr("align","center");
         					
         					var idTd=$("<td></td>").addClass("personInfo").append(item.id);
         					var personNameTd=$("<td></td>").addClass("personInfo").append(item.username);
         					personNameTd.attr("personId",item.id);
         					idTd.attr("personId",item.id);
         				
         					if(item.department !=null && item.department !=""){
         						var adminTd=$("<td></td>").append(item.department.user.username);
         						var deptNameTd=$("<td></td>").append(item.department.deptName);
         					}else{
         						var adminTd=$("<td></td>");
         						var deptNameTd=$("<td></td>");
         					}
         					
         					 
         							
         					
         					var userTypeTd=$("<td></td>").append(item.userType == '0'?"管理员":"普通员工");
         					
         					var editBtn=$("<a></a>").addClass("updatePerson").addClass("btn btn-primary").append("修改");
         					var deleteBtn=$("<a class='btn btn-danger deletePerson' >删除</a>")
         					editBtn.attr("personId",item.id);
         					deleteBtn.attr("personId",item.id);
         					deleteBtn.attr("personName",item.username);
         					var btnGroupTd=$("<td style='text-align:center;'></td>").append($("<div></div>").addClass("btn-group"));
         					
         					btnGroupTd.append(editBtn).append(deleteBtn);
         					
         					$("<tr></tr>").append(checkboxTd)
         								  .append(idTd)
         								  .append(personNameTd)
         								  .append(adminTd)
         								  .append(deptNameTd)
         								  .append(userTypeTd) 
         								  .append(btnGroupTd)
         								  .appendTo($("#personTable tbody"));
         				
         				});
         		  	}
         			 
         	 	//显示人员信息
         			$(document).on("click",".personInfo",function(){
         				var personId=$(this).attr("personId");
         				window.location.href="person/"+personId+"?target=person-info";
         			});
         			
         		//修改人员信息	
         			$(document).on("click",".updatePerson",function(){
         				var personId=$(this).attr("personId");
         				window.location.href="person/"+personId+"?target=person-revise";
         				
         			});
         			
         			//删除一条信息
         			$(document).on("click",".deletePerson",function(){
         				var personId=$(this).attr("personId");
         				var personName=$(this).attr("personName");
         				
         				if(confirm("确认删除 【 "+ personName+" 】 ？")){
         					$.ajax({
         						url:"${APP_PATH}/person/"+personId,
         						type:"DELETE",
         						success:function(result){
         							 if(result.code == 200){
         								 //显示失败消息  失败
         								 alert(result.extend.msg);
         								 //关闭模态框
         							 }else{
         								//删除成功 返回当前页
         								 to_page(currentPage);
         							 }
         						}
         					});
         				}
         				
         			});
         			
         			//删除多条消息
         			$("#deletePerson").click(function(){
         				
         				var checked=$("#personTable tbody input:checked");

         				 var userIds="";
         				 var usernames="";
         				 checked.each(function(index,value){
         					usernames += $(this).attr("username")+",";
         					userIds += $(this).attr("userId")+"-";
         				 });
         				 usernames=usernames.substring(0,usernames.length-1);
         				userIds=userIds.substring(0,userIds.length-1);

         				 if(confirm("确认要删除 【 "+usernames+" 】 吗？")){
         					 $.ajax({
         						 url:"${APP_PATH}/person/"+userIds,
         						 type:"DELETE",
         						 success:function(result){
         							 if(result.code == 200){
         								 //显示失败消息  失败
         								 alert(result.extend.msg);
         								 //关闭模态框
         							 }else{
         								//删除成功 返回当前页
         								 to_page(currentPage);
         							 }
         						 }
         					 });
         				 }
         			});

               </script>       
          </section>
      </section>
      <!--main content end-->
  </section>
  <!-- container section end -->
    <!-- javascripts -->
    <script src="js/jquery.js"></script>
   <!--  <script src="js/bootstrap.min.js"></script> -->
    <!-- nicescroll -->
    <script src="js/jquery.scrollTo.min.js"></script>
    <script src="js/jquery.nicescroll.js" type="text/javascript"></script>
    <!--custome script for all page-->
    <script src="js/scripts.js"></script>
<script	src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
  </body>
</html>
